<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>git相关设置及使用笔记 - Lglspace‘s Blog</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="lglspace" /><meta name="description" content="git常用命令 初始化仓库 1 git init git设置全局的用户名及邮箱 1 2 git config --global user.name &amp;#39;xxx&amp;#39; git config --global user.email xxx@abc.com 给特定项目指定用户名及邮箱,注：在项目目录下执行 1 2 git config user.name" /><meta name="keywords" content="lglspace, blog, lgl" />






<meta name="generator" content="Hugo 0.79.0 with theme even" />


<link rel="canonical" href="https://lglspace.cn/post/git/git-relate/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.b5a744db6de49a86cadafb3b70f555ab443f83c307a483402259e94726b045ff.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">
<link rel="stylesheet" href="/css/even-custom.css">


<meta property="og:title" content="git相关设置及使用笔记" />
<meta property="og:description" content="git常用命令 初始化仓库 1 git init git设置全局的用户名及邮箱 1 2 git config --global user.name &#39;xxx&#39; git config --global user.email xxx@abc.com 给特定项目指定用户名及邮箱,注：在项目目录下执行 1 2 git config user.name" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://lglspace.cn/post/git/git-relate/" />
<meta property="article:published_time" content="2018-11-20T10:10:10+08:00" />
<meta property="article:modified_time" content="2019-01-05T10:10:10+08:00" />
<meta itemprop="name" content="git相关设置及使用笔记">
<meta itemprop="description" content="git常用命令 初始化仓库 1 git init git设置全局的用户名及邮箱 1 2 git config --global user.name &#39;xxx&#39; git config --global user.email xxx@abc.com 给特定项目指定用户名及邮箱,注：在项目目录下执行 1 2 git config user.name">
<meta itemprop="datePublished" content="2018-11-20T10:10:10+08:00" />
<meta itemprop="dateModified" content="2019-01-05T10:10:10+08:00" />
<meta itemprop="wordCount" content="467">



<meta itemprop="keywords" content="git," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="git相关设置及使用笔记"/>
<meta name="twitter:description" content="git常用命令 初始化仓库 1 git init git设置全局的用户名及邮箱 1 2 git config --global user.name &#39;xxx&#39; git config --global user.email xxx@abc.com 给特定项目指定用户名及邮箱,注：在项目目录下执行 1 2 git config user.name"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Lglspace</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">主页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Lglspace</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">主页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">git相关设置及使用笔记</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-11-20 </span>
        <div class="post-category">
            <a href="/categories/git/"> git </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#git常用命令">git常用命令</a>
          <ul>
            <li><a href="#初始化仓库">初始化仓库</a></li>
            <li><a href="#git设置全局的用户名及邮箱">git设置全局的用户名及邮箱</a></li>
            <li><a href="#给特定项目指定用户名及邮箱注在项目目录下执行">给特定项目指定用户名及邮箱,注：在项目目录下执行</a></li>
            <li><a href="#创建本地分支">创建本地分支</a></li>
            <li><a href="#创建远程分支">创建远程分支</a></li>
            <li><a href="#切换本地分支">切换本地分支</a></li>
            <li><a href="#拉取远程分支到指定的本地分支">拉取远程分支到指定的本地分支</a></li>
            <li><a href="#删除本地分支">删除本地分支</a></li>
            <li><a href="#删除本地的远程分支">删除本地的远程分支</a></li>
            <li><a href="#删除远程服务器上的分支">删除远程服务器上的分支</a></li>
            <li><a href="#查看本地分支">查看本地分支</a></li>
            <li><a href="#查看远程分支">查看远程分支</a></li>
            <li><a href="#查看历史版本">查看历史版本</a></li>
            <li><a href="#恢复到某个版本">恢复到某个版本</a></li>
            <li><a href="#添加改变提交到仓库">添加改变，提交到仓库</a></li>
            <li><a href="#查看远程仓库地址">查看远程仓库地址</a></li>
            <li><a href="#添加到远程仓库">添加到远程仓库</a></li>
            <li><a href="#删除远程地址">删除远程地址</a></li>
            <li><a href="#强制push到远程">强制push到远程</a></li>
            <li><a href="#强制覆盖本地代码">强制覆盖本地代码</a></li>
            <li><a href="#git生成私钥与公钥">git生成私钥与公钥</a></li>
          </ul>
        </li>
        <li><a href="#git-submodule的使用">git submodule的使用</a>
          <ul>
            <li><a href="#添加子模块3">添加子模块3</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="git常用命令">git常用命令</h2>
<h3 id="初始化仓库">初始化仓库</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git init
</code></pre></td></tr></table>
</div>
</div><h3 id="git设置全局的用户名及邮箱">git设置全局的用户名及邮箱</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git config --global user.name &#39;xxx&#39;
git config --global user.email xxx@abc.com
</code></pre></td></tr></table>
</div>
</div><h3 id="给特定项目指定用户名及邮箱注在项目目录下执行">给特定项目指定用户名及邮箱,注：在项目目录下执行</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git config user.name 用户名
git config user.email 邮箱
</code></pre></td></tr></table>
</div>
</div><h3 id="创建本地分支">创建本地分支</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git branch 分支名
</code></pre></td></tr></table>
</div>
</div><h3 id="创建远程分支">创建远程分支</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">#首次提交时得加上-u参数，建立本地与远程的关联关系，之后push可以不加
git push -u origin （分支名）
</code></pre></td></tr></table>
</div>
</div><h3 id="切换本地分支">切换本地分支</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git checkout 分支名
</code></pre></td></tr></table>
</div>
</div><h3 id="拉取远程分支到指定的本地分支">拉取远程分支到指定的本地分支</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git pull &lt;远程库名&gt; &lt;远程分支名&gt;:&lt;本地分支名&gt;
</code></pre></td></tr></table>
</div>
</div><h3 id="删除本地分支">删除本地分支</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git branch -D 分支名
或
git branch --delete 分支名
</code></pre></td></tr></table>
</div>
</div><h3 id="删除本地的远程分支">删除本地的远程分支</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git branch -r -D origin/BranchName
</code></pre></td></tr></table>
</div>
</div><h3 id="删除远程服务器上的分支">删除远程服务器上的分支</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git push origin -d 分支名
或
git push origin --delete 分支名
</code></pre></td></tr></table>
</div>
</div><h3 id="查看本地分支">查看本地分支</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git branch
</code></pre></td></tr></table>
</div>
</div><h3 id="查看远程分支">查看远程分支</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git branch -a
</code></pre></td></tr></table>
</div>
</div><h3 id="查看历史版本">查看历史版本</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">git log
</code></pre></td></tr></table>
</div>
</div><h3 id="恢复到某个版本">恢复到某个版本</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">git reset --hard 44bd896bb726be3d3815f1f25d738a9cd402a477
</code></pre></td></tr></table>
</div>
</div><h3 id="添加改变提交到仓库">添加改变，提交到仓库</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">git add .
git commit -m <span class="s2">&#34;xxx&#34;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="查看远程仓库地址">查看远程仓库地址</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">git remote -v
</code></pre></td></tr></table>
</div>
</div><h3 id="添加到远程仓库">添加到远程仓库</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">git remote add origin https://github.com/XXX/YYY.git
</code></pre></td></tr></table>
</div>
</div><h3 id="删除远程地址">删除远程地址</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">git remote rm origin
</code></pre></td></tr></table>
</div>
</div><h3 id="强制push到远程">强制push到远程</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">git push -f origin develop
</code></pre></td></tr></table>
</div>
</div><h3 id="强制覆盖本地代码">强制覆盖本地代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">git fetch --all
git reset --hard origin/master
git pull
</code></pre></td></tr></table>
</div>
</div><h3 id="git生成私钥与公钥">git生成私钥与公钥</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1">#一般不设置密钥口令</span>
ssh-keygen -t rsa
<span class="c1">#查看公钥</span>
cat ~/.ssh/id_rsa.pub
</code></pre></td></tr></table>
</div>
</div><h2 id="git-submodule的使用">git submodule的使用</h2>
<h3 id="添加子模块3">添加子模块3</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">git submodule add  target-repository-url target-saved-dir
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">lglspace</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2019-01-05
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/git/">git</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/linux/linux-ssh/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">ssh免密登录</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/securecrt/securecrt-relate/">
            <span class="next-text nav-default">SecureCRT相关设置</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:lglspace@outlook.com" class="iconfont icon-email" title="email"></a>
  <a href="https://lglspace.cn/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  

  

  <span class="copyright-year">
    &copy; 
    2020 - 
    2021<span class="heart"><i class="iconfont icon-heart"></i></span><span>lglspace</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.a74e82b84b872f6b028df4e965781ddbba1606f0253b4fad5e4cf3913cf576eb.js"></script>






<script src="/js/site-custom.js"></script>


</body>
</html>
